from django.core.paginator import Paginator
from django.shortcuts import render
from rest_framework import status
from rest_framework.generics import GenericAPIView,ListAPIView
from rest_framework import status
from rest_framework.generics import GenericAPIView, RetrieveAPIView,ListAPIView
from rest_framework.generics import GenericAPIView, ListAPIView
from django_redis import get_redis_connection
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from goods.models import SKU

from .serializers import OrderCommentSerializer,GetCommentSerializer
from orders.serializers import OrderPlaceSerializer, OrderCommitSerializer
from decimal import Decimal
from rest_framework.generics import CreateAPIView
from .models import OrderGoods

class OrderPlaceView(GenericAPIView):
    serializer_class = OrderPlaceSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request):
        # 1,获取redis中需要结算的商品
        user = request.user
        redis_conn = get_redis_connection("cart")
        cart_dict = redis_conn.hgetall("cart_%s" % user.id)
        cart_selected_list = redis_conn.smembers("cart_selected_%s"%user.id)

        # 2,拼接数据
        sku_list = []
        for sku_id  in cart_selected_list:
            sku = SKU.objects.get(id=sku_id)
            sku.count = int(cart_dict[sku_id])
            sku_list.append(sku)

        # 3,获取序列化器
        serializer = self.get_serializer(instance=sku_list,many=True)

        # 4,返回响应
        contenxt = {
            "skus":serializer.data,
            "freight":Decimal(10.0)
        }
        return Response(contenxt)

class OrderCommitView(CreateAPIView):
    serializer_class = OrderCommitSerializer
    permission_classes = [IsAuthenticated]

# #获取订单里面待评价的商品
# class OrderCommentView(GenericAPIView):
#     serializer_class =OrderCommentSerializer
#
#     def get(self,request,order_id):
#         query=OrderGoods.objects.filter(order_id=order_id,is_commented=False).all()
#
#         serializers=self.get_serializer(instance=query,many=True)
#
#         # ordergoods_data=serializers.data
#
#         # for good in ordergoods_data:
#         #     sku=SKU.objects.get(id=good['id'])
#         #
#         #     seriali=SKUGoodsSerializer(instance=sku)
#         #
#         #     good["sku"]=seriali.data
#
#         return Response(data=serializers.data,status=status.HTTP_200_OK)









class OrderCommentView(GenericAPIView):
    serializer_class = OrderCommentSerializer
    permission_classes = [IsAuthenticated]
    pagination_class = None
    def get(self, request,order_id):
        # data_dict = request.query_params()
        # order_id = data_dict["order_id"]
        try:
            ordergood = OrderGoods.objects.filter(order_id=order_id, is_commented=False)
        except Exception as e:
            return Response(status=status.HTTP_400_BAD_REQUEST)

        serializer = self.get_serializer(instance=ordergood, many=True)
        return Response(serializer.data)

    def post(self,request,order_id):
        # 1 获取参数
        dict_data = request.data
        # 2 获取序列化器，校验
        serializer =self.get_serializer(data=dict_data)
        serializer.is_valid(raise_exception=True)
        # 3 数据入库
        serializer.save()
        # 4 返回
        return Response(status=status.HTTP_200_OK)

class GetCommentView(ListAPIView):
    serializer_class = GetCommentSerializer

    # queryset = OrderGoods.objects.all()
    # 重写查询集
    def get_queryset(self):
        # print(self.kwargs.get('pk'))
        order_goods = OrderGoods.objects.filter(sku_id=self.kwargs.get('pk'), is_commented=True)
        for order_good in order_goods:
            order_good.username = order_good.order.user.username
        return order_goods






